/**
 * 
 */
package vn.support.instant.communication.bot;

import java.util.HashMap;
import java.util.Map;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.packet.Message;

/**
 * @author nttuyen
 *
 */
public class GoogleBot implements AgentBot, SupportBot {

	private final String username;
	private final String password;
	private final ConnectionConfiguration config;
	
	private Map<String, Chat> chats;
	private Connection connection;
	
	public GoogleBot(String username, String password) {
		this.username = username;
		this.password = password;
		this.config = new ConnectionConfiguration("talk.google.com", 5222, "googlemail.com");
		this.chats = new HashMap<String, Chat>();
	}
	
	@Override
	public void send(String to, String message) throws Exception {
		if(!this.connection.isConnected()) {
			return;
		}
		if(!this.chats.containsKey(to)) {
			Chat chat = this.connection.getChatManager().createChat(to, new MessageListener() {
				@Override
				public void processMessage(Chat arg0, Message arg1) {
					// TODO Auto-generated method stub
					//Nhan duoc message thi tra lai channel luon?????
				}
			});
			this.chats.put(to, chat);
		}
		Chat chat = this.chats.get(to);
		chat.sendMessage(message);
	}

	@Override
	public void start() throws Exception {
		this.connection = new XMPPConnection(this.config);
		this.connection.login(this.username, this.password);
	}

	@Override
	public void stop() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
}
